﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectActualResourceHandler
{
    public class GetLastActualResourceUsage
    {
        public ProjectActualResourceUsageRepository ActualResourceUsageRepository { get; set; }
        private readonly ProjectActualResourceUsageRepository _actualResourceUsageRepository;

        #region Constructor
        public GetLastActualResourceUsage(ProjectActualResourceUsageRepository actualResourceUsageRepository)
        {
            this.ActualResourceUsageRepository = actualResourceUsageRepository;
            _actualResourceUsageRepository = actualResourceUsageRepository;
        }

        public GetLastActualResourceUsage()
        {
            _actualResourceUsageRepository = new ProjectActualResourceUsageRepository();
        }
        #endregion

        #region Methods
        public virtual ProjectActualResourceUsageModel Execute(string userId, decimal projectId, decimal taskId)
        {
            // try to get from db
            try
            {
                // convert
                ETL_ACTUAL_RESOURCE_USAGES projectActualResourceUsageData = 
                    _actualResourceUsageRepository.GetLastActualResourceUsage(userId, projectId, taskId);

                return new ProjectActualResourceUsageModel(projectActualResourceUsageData);
            }
            catch (Exception ex)
            {
                
                LogService.Service.Debug(ex);
                return null;
            }


        }

        public virtual ProjectActualResourceUsageModel Execute(string userId)
        {
            // try to get from db
            try
            {
                // convert
                ETL_ACTUAL_RESOURCE_USAGES projectActualResourceUsageData =
                    _actualResourceUsageRepository.GetLastActualResourceUsage(userId);
                if (projectActualResourceUsageData != null)
                    return new ProjectActualResourceUsageModel(projectActualResourceUsageData);
                else
                    return null;
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
                return null;
            }


        }
        #endregion
    }
}
